package solitaire.presentation;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import solitaire.commandes.Command;

/**
 * Impl&eacute;mentation de l'IHM du jeu.
 * 
 * @author Andr&eacute; Simon, J&eacute;r&eacute;my Brault
 */
public class GameWindow extends JFrame implements GameWindowInterface {

	private static final long serialVersionUID = 3946024893655701346L;

	private JPanel solitaire;

	private JMenuBar menuBar;
	
	JMenu menuGame;
	JMenuItem menuRestart;
	JMenuItem menuQuit;
	
	private MenuRestartActionListener restartActionListener;
	private MenuQuitActionListener quitActionListener;
	
	private Command restartGameCommand;
	private Command quitGameCommand;
	
	public GameWindow() {		
		menuBar = new JMenuBar();
		
		menuGame = new JMenu("Jeu");
		menuRestart = new JMenuItem("Nouvelle donne");
		menuQuit = new JMenuItem("Quitter");
		
		menuGame.add(menuRestart);
		menuGame.add(menuQuit);

		menuBar.add(menuGame);

		setJMenuBar(menuBar);

		solitaire = new JPanel();
	
		restartActionListener = new MenuRestartActionListener();
		quitActionListener = new MenuQuitActionListener();
		
		menuRestart.addActionListener(restartActionListener);
		menuQuit.addActionListener(quitActionListener);
		
		menuGame.setMnemonic(KeyEvent.VK_J);
		menuRestart.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
		menuQuit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK));
	}
	
	@Override
	public void setRestartGameCommand(Command restartGameCommand) {
		this.restartGameCommand = restartGameCommand;
	}
	
	@Override
	public void setQuitGameCommand(Command quitGameCommand) {
		this.quitGameCommand = quitGameCommand;
	}
	
	@Override
	public void displayGame(PSolitaire solitaire) {
		this.solitaire = solitaire;
		setContentPane(this.solitaire);
		validate();
		repaint();
	}
	
	@Override
	public void quitGame() {
		System.exit(0);
	}
	
	class MenuRestartActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			restartGameCommand.execute();
		}
		
	}

	class MenuQuitActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			quitGameCommand.execute();
		}
		
	}
	
}
